package com.knowledge.plus.http.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.knowledge.plus.http.demo.pojo.Staff;
import com.knowledge.plus.http.demo.pojo.StaffRes;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author PF.Tian
 * @since 2021/09/13
 */
@Slf4j
public class HttpDemo {
    @Test
    public void getOperationStaffData() {
        log.info("RestTemplate access operation platform");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String token = "eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJzeXM6ZWRpdCxwcm9qZWN0OmhpZGRlbixibHQ6bGlzdCxwcm9qZWN0LHByb2plY3Q6ZmluZCxyb2xlOmFkZCxzZXR0aW5nLGFsYXJtU2V0dGluZzphZGQsYmxnOmV4cG9ydCxyb2xlOmRlbCxjYXJkOmxpc3QsYWxhcm1TZXR0aW5nOmRlbCx1c2VyOmZpbmQscHJvamVjdDpsaXN0LGFsYXJtOmZpbmQsc3lzOmxpc3Qsc3lzdGVtLGFsYXJtU2V0dGluZzpmaW5kLGNhcmQ6ZmluZCxkZXZpY2UsY2FyZDphZGQsdXNlcjpsaXN0LGFsYXJtU2V0dGluZzplZGl0LGJ1aWxkU2V0dGluZzpkZWwsYWxhcm06bGlzdCxidWlsZFNldHRpbmc6ZmluZCxyb2xlOmZpbmQsdXNlcjpkZWwsYmxnOmZpbmQsc21zOmxpc3Qsc3lzOmZpbmQsYWxhcm06aGFuZGxlLGNhcmQ6ZGVsLHJvbGU6bGlzdCxhbGFybSxhbGFybVNldHRpbmc6bGlzdCxibGc6bGlzdCxjYXJkOmVkaXQsdXNlcjphZGQsYnVpbGRTZXR0aW5nOmxpc3QsYmx0OmZpbmQscHJvamVjdDpkZXZpY2UscHJvamVjdDpkZXRhaWwsdXNlcjplZGl0LHNtczpmaW5kLHJvbGU6ZWRpdCx1c2VyOnJlc2V0LGJ1aWxkU2V0dGluZzplZGl0LGJ1aWxkU2V0dGluZzphZGQiLCJqdGkiOiJjZDBjMGFlOC1mYTgxLTQ1M2UtOGUxMi1jNzAyYzcyOGEyNDUifQ.mOzuwTX4WamCwQMck9ryIzYZxXj3J6NOtjvV6f_PHTwrrtDnZKeFGLczLWQVh2JvDG55ogpqAVR9TSDnqFE8KQ";
        headers.add("Authorization", token);
        ResponseEntity response = restTemplate.exchange(
                "http://127.0.0.1/api-operation/user/findPageData?userName=&current=1&limit=999",
                HttpMethod.GET,
                new HttpEntity<String>(headers),
                Map.class
        );
        log.info("Exit operation platform");
        Object body = response.getBody();
        ObjectMapper objectMapper = new ObjectMapper();
        List<Staff> staffList = null;
        if (body != null) {
            try {
                StaffRes forObject = objectMapper.convertValue(body, StaffRes.class);
                if (forObject.getErrorCode() == 0 && !forObject.getData().getDatas().isEmpty()) {
                    staffList = forObject.getData().getDatas();
                }
            } catch (IllegalArgumentException e) {
                log.error("GetOperationStaffData IllegalArgumentException");
                e.printStackTrace();
            }
            log.info("getOperationStaffData success");
        }
        System.out.println(staffList);
    }

}

